/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package demogroup5server.data;

import entity.Product;
import interfaces.LoadProductInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DungTV
 */
public class LoadProduct extends UnicastRemoteObject implements LoadProductInterface{
    private final String sqlStmtLoadAllProduct = "select * from Product";

    public LoadProduct() throws RemoteException{
        super();
    }

    public ArrayList<Product> loadAllProduct() throws RemoteException {
        
        Connect con = Connect.getInstance();
        try {
            con.connectDatabase();
            PreparedStatement pStmtLoadAllProduct = con.getConnection().prepareStatement(sqlStmtLoadAllProduct);
            ResultSet rs = pStmtLoadAllProduct.executeQuery();
            return loadAllProductByResultSet(rs);
           
        } catch (SQLException ex) {
            Logger.getLogger(LoadProduct.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    

    private ArrayList<Product> loadAllProductByResultSet(ResultSet rs) {
         ArrayList<Product> arr = new ArrayList<Product>();
        
         try {
            while (rs.next()) {
                Product procduct = new Product(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getLong(6), rs.getDouble(7), rs.getString(8), rs.getString(9), rs.getBoolean(10));
                arr.add(procduct);
            }            
        } catch (SQLException ex) {
            Logger.getLogger(LoadProduct.class.getName()).log(Level.SEVERE, null, ex);
        }
         return arr;
    }

}
